﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace UI
{
    public partial class WatermarkTextBox : UserControl
    {
        public WatermarkTextBox()
        {
            InitializeComponent();
        }

        public Brush WatermarkTextForeground { get; set; }
        public string WatermarkText { get; set; }
        public TextWrapping TextWrapping { get; set; }

        public string Text 
        { 
            get
            {
                return this.textBox.Text;
            }
            set 
            {
                this.textBox.Text = value;
            }
        }

        private bool showWatermark = true;

        private void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (showWatermark == true)
            {
                showWatermark = false;
                this.textBlock.Opacity = 0;
                OnWatermarkHidden();
            }
        }

        private void textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.textBox.Text == string.Empty)
            {
                showWatermark = true;
                this.textBlock.Opacity = 1;
                OnWatermarkShown();
            }
        }

        public void ReInite()
        {
            bool reinitByLostFocus = false;
            if (FocusManager.GetFocusedElement() == this.textBox)
            {
                this.Unfocus();
                if (this.textBox.Text == string.Empty)
                {
                    reinitByLostFocus = true;
                }
            }

            if (reinitByLostFocus == false)
            {
                this.textBox.Text = string.Empty;
                showWatermark = true;
                this.textBlock.Opacity = 1;
                OnWatermarkShown();
            }
        }

        private void Unfocus()
        {
            DependencyObject control = this.textBox;
            do
            {
                control = VisualTreeHelper.GetParent(control);
                if (control is PhoneApplicationPage)
                {
                    (control as PhoneApplicationPage).Focus();
                    break;
                }
            }
            while (control != null);
        }

        public bool IsFocused
        {
            get
            {
                if (FocusManager.GetFocusedElement() == this.textBox)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (TextChanged != null)
            {
                TextChanged(sender, e);
            }
        }

        private void OnWatermarkShown()
        {
            if (WatermarkShown != null)
            {
                WatermarkShown(this, EventArgs.Empty);
            }
        }

        private void OnWatermarkHidden()
        {
            if (WatermarkHidden != null)
            {
                WatermarkHidden(this, EventArgs.Empty);
            }
        }

        public event TextChangedEventHandler TextChanged;
        public event EventHandler WatermarkShown;
        public event EventHandler WatermarkHidden;
    }
}
